package com.app.lxx.pregnancyberth.widge.Recycler;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.lxx.pregnancyberth.R;
import com.squareup.picasso.Picasso;

import java.util.List;


/**
 * Created by Lxx on 2018/4/9.
 */

public abstract class MyRecycleAdapter<T> extends RecyclerView.Adapter {

    private static final int TYPE_ITEM = 10;
    private static final int TYPE_FOOTER = 11;
    private List<T> list;//数据集合
    private int itemLayout;//item的布局
    private Context context;
    private boolean isNeedMore;//是否需要加载更多的view

    public boolean isNeedMore() {
        return isNeedMore;
    }

    public void setIsNeedMore(boolean isNeedMore) {
        this.isNeedMore = isNeedMore;
    }

    /**
     * item的点击和长按监听
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public MyRecycleAdapter(Context context, List<T> list, int itemLayout, boolean isNeedMore) {
        this.context = context;
        this.list = list;
        this.itemLayout = itemLayout;
        this.isNeedMore = isNeedMore;
    }

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

    public int getItemLayout() {
        return itemLayout;
    }

    public void setItemLayout(int itemLayout) {
        this.itemLayout = itemLayout;
    }

    @Override
    public int getItemViewType(int position) {
        if (!isNeedMore) {
            return super.getItemViewType(position);
        }
        if (position + 1 == getItemCount()) {
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.latoyt_recycler_foot_view, parent, false);
            return new FooterViewHolder(inflate);
        } else {
            View view = LayoutInflater.from(context).inflate(itemLayout, parent, false);
            return new MyViewHolder(view);
        }
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (TYPE_ITEM == getItemViewType(position) || super.getItemViewType(position) == getItemViewType(position)) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setPositionClick(position);
                }
            });
            //长按监听
            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    return false;
                }
            });
            initData((MyViewHolder) holder, position);

        }
//            holder.itemView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    int position = holder.getLayoutPosition();
//                    onItemClickListener.onItemClick(holder.itemView, position);
//                }
//            });
//            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
//                @Override
//                public boolean onLongClick(View v) {
//                    int position = holder.getLayoutPosition();
//                    onItemClickListener.onItemLongClick(holder.itemView, position);
//                    return false;
//                }
//            });

    }

    @Override
    public int getItemCount() {
        if (isNeedMore) {
            return list.size() == 0 ? 0 : list.size() + 1;
        } else {
            return list.size();
        }
    }

    protected abstract void setPositionClick(int position);

    protected abstract void initData(MyViewHolder holder, int position);

    public class MyViewHolder extends RecyclerView.ViewHolder {

        private View findView;
        private View convertView;
        private SparseArray<Object> tags;

        public MyViewHolder(View itemView) {
            super(itemView);
            this.convertView = itemView;
            this.findView = itemView;
            tags = new SparseArray<>();
        }

        public View getConverView() {
            return convertView;
        }

        public View getFindView() {
            return findView;
        }

        @RequiresApi(api = Build.VERSION_CODES.DONUT)
        public <T extends View> T getView(@IdRes int viewId) {
            View view;
            Object viewObj = findView.getTag(viewId);
            if (null != viewObj) {
                view = (View) viewObj;
            } else {
                view = findView.findViewById(viewId);
                findView.setTag(viewId, view);
            }
            return (T) view;
        }

        /**
         * 根据 viewId 获取一个 ImageView 对象
         */
        public ImageView getImageView(@IdRes int viewId) {
            return (ImageView) getView(viewId);
        }


        /**
         * 根据 viewId 获取一个 CheckBox 对象
         */
        public CheckBox getCheckBox(@IdRes int viewId) {
            return (CheckBox) getView(viewId);
        }

        /**
         * 为指定 viewId 的 ImageView 对象设置图片
         */
        public MyViewHolder setImageDrawable(@IdRes int viewId, @Nullable Drawable drawable) {
            if (getImageView(viewId) != null) {
                getImageView(viewId).setImageDrawable(drawable);
            }
            return this;
        }

        /**
         * 为指定 viewId 的 ImageView 对象设置图片
         */
        public MyViewHolder setImageResource(@IdRes int viewId, @DrawableRes int resId) {
            if (getImageView(viewId) != null) {
                getImageView(viewId).setImageResource(resId);
            }
            return this;
        }

        public MyViewHolder setImagePicasso(@IdRes int viewId, Context context, String url) {
            if (getImageView(viewId) != null) {
                if (TextUtils.isEmpty(url)) {
                    Picasso.with(context).load(R.drawable.ic_merchant_logo).into(getImageView(viewId));
                } else {
                    Picasso.with(context).load(url).fit().centerCrop()
                            .placeholder(R.drawable.ic_merchant_logo)
                            .error(R.drawable.ic_merchant_logo)
                            .into(getImageView(viewId));
                }
            }
            return this;
        }


        /**
         * 为指定 viewId 的 ImageView 对象设置图片
         */
        public MyViewHolder setImageBitmap(@IdRes int viewId, Bitmap bitmap) {
            if (getImageView(viewId) != null) {
                getImageView(viewId).setImageBitmap(bitmap);
            }
            return this;
        }

        /**
         * 根据 viewId 获取一个 TextView 对象
         */
        public TextView getTextView(@IdRes int viewId) {
            return (TextView) getView(viewId);
        }


        /**
         * 为指定 viewId 的 TextView 对象设置文字
         */
        public MyViewHolder setText(@IdRes int viewId, CharSequence text) {
            if (getTextView(viewId) != null) {
                getTextView(viewId).setText(text);
            }
            return this;
        }

        /**
         * 为指定 viewId 的 TextView 对象设置文字颜色
         */
        public MyViewHolder setTextColor(@IdRes int viewId, @ColorInt int color) {
            if (getTextView(viewId) != null) {
                getTextView(viewId).setTextColor(color);
            }
            return this;
        }

        /**
         * 为指定 viewId 的 TextView 对象设置文字size
         */
        public MyViewHolder setTextSize(@IdRes int viewId, @ColorInt int size) {
            if (getTextView(viewId) != null) {
                getTextView(viewId).setTextSize(size);
            }
            return this;
        }

        /**
         * 为指定 viewId 的 TextView 对象设置北京颜色
         */
        @SuppressLint("ResourceType")
        public MyViewHolder setTextBackGround(@IdRes int viewId, @ColorInt int backGround) {
            if (getTextView(viewId) != null) {
                getTextView(viewId).setBackgroundResource(backGround);
            }
            return this;
        }

        /**
         * 为指定 viewId 的 TextView 对象设置北京颜色
         */
        public MyViewHolder setTextBackGroundColor(@IdRes int viewId, @ColorInt int color) {
            if (getTextView(viewId) != null) {
                getTextView(viewId).setBackgroundColor(color);
            }
            return this;
        }

        /**
         * 为指定 viewId 的 TextView 对象 添加删除线
         */
        public MyViewHolder setDeleteLines(@IdRes int viewId) {
            if (getView(viewId) != null) {
                TextView textView = (TextView) getView(viewId);
                textView.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
            }
            return this;
        }

        /**
         * 为指定 viewId 的 View 对象设置 color
         */
        public MyViewHolder setViewBackgroundColor(@IdRes int viewId, final String color) {
            if (getView(viewId) != null) {
                getView(viewId).setBackgroundColor(Color.parseColor(color));
            }
            return this;
        }

        /**
         * 为指定 viewId 的 View 对象设置 TAG
         */
        public MyViewHolder setTag(@IdRes int viewId, final Object tag) {
            if (getView(viewId) != null) {
                getView(viewId).setTag(tag);
            }
            return this;
        }

        /**
         * 使用当前 ViewHolder 记录一个 TAG
         */
        public MyViewHolder putTag(int key, final Object tag) {
            tags.put(key, tag);
            return this;
        }

        /**
         * 从当前 ViewHolder 中取出一个TAG
         */
        public Object getTag(int key) {
            return tags.get(key);
        }

        /**
         * 为指定 viewId 的 CheckBox 对象设置选中状态
         */
        public MyViewHolder setChecked(@IdRes int viewId, boolean checked) {
            if (getCheckBox(viewId) != null) {
                getCheckBox(viewId).setChecked(checked);
            }
            return this;
        }

        /**
         * 切换指定 viewId 的 CheckBox 的选中状态
         */
        public MyViewHolder toggle(@IdRes int viewId) {
            if (getCheckBox(viewId) != null) {
                getCheckBox(viewId).toggle();
            }
            return this;
        }

        /**
         * 为指定 viewId 的 View 对象设置点击监听
         */
        public MyViewHolder setOnClickListener(@IdRes int viewId, @Nullable View.OnClickListener listener) {
            if (getView(viewId) != null) {
                getView(viewId).setOnClickListener(listener);
            }

            return this;
        }

        public MyViewHolder setOnLongClickListener(@IdRes int viewId, @Nullable View.OnLongClickListener listener) {
            if (getView(viewId) != null) {
                getView(viewId).setOnLongClickListener(listener);
            }

            return this;
        }

        /**
         * 为指定 viewId 的 View 对象设置是否可见
         */
        public MyViewHolder setVisibile(@IdRes int viewId, boolean visible) {
            if (getView(viewId) != null) {
                getView(viewId).setVisibility(visible ? View.VISIBLE : View.GONE);
            }
            return this;
        }

        /**
         * 设置指定的id视图的可见性
         *
         * @param viewId
         * @param visibility
         * @return
         */
//        public MyViewHolder setViewVisibility(@IdRes int viewId, int visibility) {
//            if (getView(viewId) != null) {
//                getView(viewId).setVisibility(visibility);
//            }
//            return this;
//        }


        /**
         * 为指定 viewId 的 View 对象设置布局参数
         */
        public MyViewHolder setLayoutParams(@IdRes int viewId, RelativeLayout.LayoutParams params) {
            if (getView(viewId) != null) {
                getView(viewId).setLayoutParams(params);
            }
            return this;
        }
    }

    /**
     * 移除某一条记录
     *
     * @param position 移除数据的position
     */
    public void removeItem(int position) {
        if (position < list.size()) {
            list.remove(position);
            notifyItemRemoved(position);
        }
    }

    /**
     * 添加一条记录
     *
     * @param data     需要加入的数据结构
     * @param position 插入位置
     */
    public void addItem(T data, int position) {
        if (position <= list.size()) {
            list.add(position, data);
            notifyItemInserted(position);
        }
    }

    /**
     * 添加一条记录
     *
     * @param data 需要加入的数据结构
     */
    public void addItem(T data) {
        addItem(data, list.size());
    }

    /**
     * 移除所有记录
     */
    public void clearItems() {
        int size = list.size();
        if (size > 0) {
            list.clear();
            notifyItemRangeRemoved(0, size);
        }
    }

    /**
     * 批量添加记录
     *
     * @param data     需要加入的数据结构
     * @param position 插入位置
     */
    public void addItems(List<T> data, int position) {
        if (position <= list.size() && data != null && data.size() > 0) {
            list.addAll(position, data);
            notifyItemRangeChanged(position, data.size());
        }
    }

    /**
     * 批量添加记录
     *
     * @param data 需要加入的数据结构
     */
    public void addItems(List<T> data) {
        addItems(data, list.size());
    }

    public void resetItems(List<T> data) {
        clearItems();
        addItems(data);
    }

    public class FooterViewHolder extends RecyclerView.ViewHolder {

        private TextView foot_tv;

        public FooterViewHolder(View itemView) {
            super(itemView);
            foot_tv = itemView.findViewById(R.id.foot_tv);
        }
    }
}
